package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 11:21
 */
@RestController
@RequestMapping("/brand")
@Api(value = "品牌制造商：列表查询、添加、删除、修改")
//解决前后端跨域访问问题
@CrossOrigin("*")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 品牌制造商列表
     * @param id    根据品牌商id查询
     * @param name  根据品牌商名字查询
     * @param page  当前页，默认1
     * @param limit 每页显示多少条，默认20条
     * @param sort  排序字段名，默认按照add_time添加时间排序
     * @param order 排序方式，默认desc降序
     * @return
     */
    @GetMapping("/list")
    public Object brandList(Integer id , String name ,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            @RequestParam(defaultValue = "add_time") String sort,
                            @RequestParam(defaultValue = "desc") String order){

        //调用service业务方法
        IPage<DtsBrand> resultPage = brandService.queryBrandPage(id, name, page, limit, sort, order);

        //获取查询的数据
        List<DtsBrand> list = resultPage.getRecords();
        //获取数据总条数
        long total = resultPage.getTotal();

        //封装数据并返回数据
        Map<String , Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",list);

        return ResponseUtil.ok(data);

    }

    /**
     *  品牌和管理员列表
     * @return
     */
    @GetMapping("/catAndAdmin")
    public Map<String , Object> catAndAdmin(){

       //获取管理员数据
        List<DtsAdmin> adminList = adminService.findAdminList();
        //获取一级分类数据
        List<DtsCategory> categoryList = categoryService.selectCategorylist();
        //获取二级分类数据
        List<DtsCategory> L2List = categoryService.selectCategoryL2();

        List<CatVo> adminVo = new ArrayList<>();
        for ( DtsAdmin admin : adminList ){
            CatVo vo = new CatVo();
            vo.setValue(admin.getId());
            vo.setLabel(admin.getUsername());
            adminVo.add(vo);
        }
        List<CatVo> cateList = cate(categoryList, L2List);

        //封装管理员、分类数据
        Map<String , Object> data = new HashMap<>();
        data.put("adminList",adminVo);
        data.put("categoryList",cateList);

        return ResponseUtil.ok(data);
    }

    /**
     *添加
     * @param brand
     * @return
     */
    @PostMapping("/create")
    public Object Add(@RequestBody DtsBrand brand){
        int count = brandService.add(brand);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(brand);
    }

    /**
     * 修改品牌信息
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand){
        int count = brandService.update(brand);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(brand);
    }

    /**
     * 删除品牌信息
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        int count = brandService.delete(brand);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(brand);
    }

    /**
     * 封装分类
     * @param categoryList
     * @param L2List
     * @return
     */
    public List<CatVo> cate(List<DtsCategory> categoryList , List<DtsCategory> L2List){

        List<CatVo> adminVo = new ArrayList<>();
        //遍历一级类目
        for ( DtsCategory L1 : categoryList ){
            CatVo voL1 = new CatVo();
            List<CatVo> L2Cat = new ArrayList<>();
            if (L1.getPid()== 0){
                //遍历二级类目
                for (DtsCategory L2 : L2List){
                    CatVo voL2 = new CatVo();
                    if (L2.getPid() != 0 && L2.getPid().equals(L1.getId()) ){
                         voL2.setValue(L2.getId());
                         voL2.setLabel(L2.getName());

                         L2Cat.add(voL2);
                    }
                }
                voL1.setValue(L1.getId());
                voL1.setLabel(L1.getName());
                voL1.setChildren(L2Cat);

                adminVo.add(voL1);
            }
        }
        return adminVo;
    }

}
